﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;


namespace pinlib.functions
{
    /// <summary>
    /// Callback functions - both managed and unmanaged
    /// </summary>
    public static class Callbacks
    {
        /// <summary>
        ///   An application-defined callback function used with the EnumPageFiles function.
        /// </summary>
        /// <param name = "pContext">The user-defined data passed from EnumPageFiles (the List in this case)</param>
        /// <param name = "pageFileInfo">A pointer to an ENUM_PAGE_FILE_INFORMATION structure</param>
        /// <param name = "lpFilename">The name of the pagefile</param>
        /// <returns>true to continue, false to exit</returns>
        internal delegate Boolean EnumPageFilesProc(
            IntPtr pContext, ref ENUM_PAGE_FILE_INFORMATION pageFileInfo, string lpFilename);

        /// <summary>
        ///   An application-defined (managed) callback function used with the EnumPageFiles function.
        /// </summary>
        /// <param name = "pContext">The user-defined data passed from EnumPageFiles (the List in this case)</param>
        /// <param name = "pPageFileInfo">A pointer to an ENUM_PAGE_FILE_INFORMATION structure</param>
        /// <param name = "lpFilename">The name of the pagefile</param>
        /// <returns>true to continue, false to exit</returns>
        /// <remarks>
        ///   In this case, we're using the pContext to hold the pointer to the List of ENUM_PAGE_FILE_INFORMATION's
        /// </remarks>
        internal static Boolean EnumPageFilesProcM(IntPtr pContext, ref ENUM_PAGE_FILE_INFORMATION pPageFileInfo,
                                                   string lpFilename)
        {
            // get the managed from the unmanaged 
            GCHandle gch = GCHandle.FromIntPtr(pContext);
            List<ENUM_PAGE_FILE_INFORMATION> tmpPfi = gch.Target as List<ENUM_PAGE_FILE_INFORMATION>;

            tmpPfi.Add(pPageFileInfo);
            return true;
        }
    }
}
